// models/score.go
package models

import (
	"time"

	"gorm.io/gorm"
)

// Score 成绩模型
type Score struct {
	ID        uint           `gorm:"primaryKey" json:"id"`
	UserID    uint           `gorm:"index;not null" json:"user_id"`
	SubjectID uint           `gorm:"index;not null" json:"subject_id"`
	Score     float64        `gorm:"not null" json:"score"`
	ExamDate  time.Time      `json:"exam_date"`
	Remarks   string         `gorm:"size:500" json:"remarks"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`

	// 关联
	User    User    `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Subject Subject `gorm:"foreignKey:SubjectID" json:"subject,omitempty"`
}

// ScoreResponse 用于 API 响应的成绩数据
type ScoreResponse struct {
	ID          uint      `json:"id"`
	UserID      uint      `json:"user_id"`
	SubjectID   uint      `json:"subject_id"`
	Score       float64   `json:"score"`
	ExamDate    time.Time `json:"exam_date"`
	Remarks     string    `json:"remarks"`
	UserName    string    `json:"user_name,omitempty"`
	SubjectName string    `json:"subject_name,omitempty"`
}

// ToResponse 将成绩模型转换为响应格式
func (s *Score) ToResponse() ScoreResponse {
	return ScoreResponse{
		ID:          s.ID,
		UserID:      s.UserID,
		SubjectID:   s.SubjectID,
		Score:       s.Score,
		ExamDate:    s.ExamDate,
		Remarks:     s.Remarks,
		UserName:    s.User.Name,
		SubjectName: s.Subject.Name,
	}
}
